var userid;
var have
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zhulishow: false,
    huanxingshow: false,
    rulepopshow: false,
    alllist: [],
    huanxinglist: [],
    fisthead: "",
    secondhead: "",
    thirdhead: "",
    fourhead: "",
    fivehead: "",
    sixhead: "",
    fistname: "",
    secondname: "",
    thirdname: "",
    fourname: "",
    fivename: "",
    sixname: "",
    first: false,
    second: false,
    third: false,
    laoimg: "../../images/earned_cash/lao.png",
    tieimg: "../../images/earned_cash/tie.png",
    zhuimg: "../../images/earned_cash/zhu.png",
    zhuimg: "../../images/earned_cash/zhu.png",
    niimg: "../../images/earned_cash/ni.png",
    zhuanimg: "../../images/earned_cash/zhuan.png",
    xianjinimg: "../../images/earned_cash/xianjin.png",
    nativeimg: "../../images/earned_cash/packet.png",
    positiveimg: "../../images/earned_cash/packet-active.png",
    canshare: true,
    authbg: getApp().globalData.authImg,
    authbtn2: getApp().globalData.source + "red_packet/btn2.png",
    authdialog: getApp().globalData.source + "dialog/dialog.png",
    authbtn: false
  },


  authbtn: function () {
    var vm = this
    wx.showLoading({
      title: "正在检查网络.."
    })
    this.setData({
      authbtn: true
    })
  },
  bindGetUserInfo: function (e) {
    getApp().authInfo(e);
  },

  todetails: function() {
    wx.navigateTo({
      url: '/pages/my/mydetail/mydetail',
    })
  },

  getbannertext: function() {
    var that = this;
    //加载首页banner数据
    getApp().request(getApp().globalData.https + "programResource/scrolTextList",
      "get", "",
      function(res) {
        if (res.code == 0) {
          // 获取系统信息  
          that.setData({
            barrageList1: res.data,
          });
        }
      },
      function(res) {
        console.log(res)
      }
    )
  },

  rulePopShow: function() {
    var that = this
    this.setData({
      rulepopshow: !that.data.rulepopshow
    })
  },
  rulePopHide: function(e) {
    var that = this
    this.setData({
      rulepopshow: !that.data.rulepopshow
    })

  },

  zhuLiShow: function() {
    var that = this
    this.setData({
      zhulishow: !that.data.zhulishow
    })
  },
  huanXingShow: function() {
    var that = this
    this.setData({
      huanxingshow: !that.data.huanxingshow
    })
  },
  gethuanxinglist: function() {
    var that = this;
    var data = {
      "userId": userid,
    }
    getApp().requestLoading(getApp().globalData.https + "activityResource/wakeUser/" + userid,
      "get", data, "加载中...",
      function(res) {
        console.log(JSON.stringify(res))
        if (res.code == 0) {
          that.setData({
            huanxinglist: res.data
          });
        }
      },
      function(res) {
        console.log(res)
      }
    )
  },
  empty: function () { },

  openpkg: function() {
    if (this.data.pkgdata.unclaimed == 0) {
      getApp().showMsgToast("邀请好友才能领红包哦..")
      return
    }
    if (this.data.pkgdata.obtainPkg >= 2) {
      getApp().showMsgToast("已领完..")
      return
    }
    var that = this;
    var data = {
      "userId": userid,
    }
    getApp().requestLoading(getApp().globalData.https + "/activityResource/inviteRedPkgGet",
      "get", data, "加载中...",
      function(res) {
        console.log(JSON.stringify(res))
        if (res.code == 0) {
          that.getRedpkg()
        }
      },
      function(res) {
        console.log(res)
      }
    )
  },
  getRedpkg: function() {
    var that = this;
    var data = {
      "userId": userid,
    }
    getApp().requestLoading(getApp().globalData.https + "/activityResource/inviteRedPkg",
      "get", data, "加载中...",
      function(res) {
        console.log(JSON.stringify(res))
        if (res.code == 0) {
          that.setData({
            pkgdata: res.data
          });
          if (res.data.unclaimed > 0) {
            that.setData({
              canshare: false
            });
          } else {
            that.setData({
              canshare: true
            });
          }

        }
      },
      function(res) {
        console.log(res)
      }
    )
  },

  getall: function() {
    var that = this;
    var data = {
      "userId": userid
    }
    getApp().request(getApp().globalData.https + "activityResource/signStatus/" + userid,
      "get", data,
      function(res) {
        if (res.code == 0) {
          if (res.data.length == 2) {
            that.setData({
              alllist: res.data,
              fisthead: res.data[0][0].headPortraits[0],
              secondhead: res.data[0][0].headPortraits[1],
              thirdhead: res.data[1][0].headPortraits[0],
              fourhead: res.data[1][0].headPortraits[1],
              // fivehead: res.data[2][0].headPortraits[0],
              // sixhead: res.data[2][0].headPortraits[1],
              fistname: res.data[0][0].nicknames[0],
              secondname: res.data[0][0].nicknames[1],
              thirdname: res.data[1][0].nicknames[0],
              fourname: res.data[1][0].nicknames[1],
              first: true,
              second: true,
              third: false,
              // fivename: res.data[2][0].nicknames[0],
              // sixname: res.data[2][0].nicknames[1],
            });
          } else if (res.data.length == 3) {
            that.setData({
              alllist: res.data,
              fisthead: res.data[0][0].headPortraits[0],
              secondhead: res.data[0][0].headPortraits[1],
              thirdhead: res.data[1][0].headPortraits[0],
              fourhead: res.data[1][0].headPortraits[1],
              fivehead: res.data[2][0].headPortraits[0],
              sixhead: res.data[2][0].headPortraits[1],
              fistname: res.data[0][0].nicknames[0],
              secondname: res.data[0][0].nicknames[1],
              thirdname: res.data[1][0].nicknames[0],
              fourname: res.data[1][0].nicknames[1],
              fivename: res.data[2][0].nicknames[0],
              sixname: res.data[2][0].nicknames[1],
              first: true,
              second: true,
              third: true,
            });
          } else if (res.data.length == 1) {
            that.setData({
              alllist: res.data,
              fisthead: res.data[0][0].headPortraits[0],
              secondhead: res.data[0][0].headPortraits[1],
              // thirdhead: res.data[1][0].headPortraits[0],
              // fourhead: res.data[1][0].headPortraits[1],
              // fivehead: res.data[2][0].headPortraits[0],
              // sixhead: res.data[2][0].headPortraits[1],
              fistname: res.data[0][0].nicknames[0],
              secondname: res.data[0][0].nicknames[1],
              // thirdname: res.data[1][0].nicknames[0],
              // fourname: res.data[1][0].nicknames[1],
              // fivename: res.data[2][0].nicknames[0],
              // sixname: res.data[2][0].nicknames[1],
              first: true,
              second: false,
              third: false,
            });
          } else {
            that.setData({
              alllist: res.data,
              first: false,
              second: false,
              third: false,
            });
          }
        } else {
          have = true;
        }
      },
      function(res) {
        console.log(res)
      }
    )
  },


  packetsPopShow: function(e) {
    // wx.showLoading({
    //   title: '稍等..',
    // })
    console.log("走到这里了 .")
    var that = this
    var row = e.currentTarget.dataset.row;
    var column = e.currentTarget.dataset.column;
    // 没有数据
    var can = false;
    switch (row) {
      case "1":
        //第一组
        can = this.canGet(that.data.first, row, column)
        break
      case "2":
        //第一组
        can = this.canGet(that.data.second, row, column)
        break
      case "3":
        //第一组
        can = this.canGet(that.data.third, row, column)
        break
    }
    if (can) {
      this.reccord(column, row)
    }

  },

  canGet: function(check, row, column) {
    //第一组

    var data = this.data.alllist;

    if (check) {
      //有数据 ，判断可领取状态
      if (data[row - 1][column - 1].radPkgStatus == 0) {
        // 灰色状态
        getApp().showMsgToast("好友收听60分钟后才有红包领～")
        return false
      } else {
        //红色状态 ，判断可以领取的个数
        if (data[row - 1][column - 1].radPkgStatus == 0) {
          // 灰色状态
          getApp().showMsgToastLong("好友收听60分钟后才有红包领～")
          return false
        } else {
          //红色状态 ，判断可以领取的个数
          if (data[row - 1][column - 1].totalRad == 0) {
            getApp().showMsgToastLong("听节目得听币,记得提醒好友")
            return false
          } else if (data[row - 1][column - 1].totalRad == 1) {
            return true
          } else {
            getApp().showMsgToastLong("听节目得听币,记得提醒好友")
            return true
          }
        }
      }
    } else {
      //没有数据
      getApp().showMsgToastLong("邀请好友收听领红包～")
      return false
    }
  },

  reccord: function(column, row) {
    var that = this;
    console.log(JSON.stringify(this.data.alllist[column - 1][row - 1].userIds));
    if (this.data.alllist[column - 1][row - 1].userIds.length == 1) {
      var data = {
        "tarUserIds": this.data.alllist[column - 1][row - 1].userIds[0],
        "num": this.data.alllist[column - 1][row - 1].day
      }
    } else {
      var data = {
        "tarUserIds": this.data.alllist[column - 1][row - 1].userIds[0] + "," + this.data.alllist[column - 1][row - 1].userIds[1],
        "num": this.data.alllist[column - 1][row - 1].day
      }
    }

    getApp().request(getApp().globalData.https + "activityResource/postSignStatus/" + userid,
      "get", data,
      function(res) {
        // that.getfriendslist()
        that.getall()

        if (that.data.alllist[row - 1][column - 1].totalRad == 1){
          getApp().showMsgToastLong(res.data.balance + "元已到账,还有一个红包哦！再接再厉～")
        }
      },
      function(res) {
        console.log(res)
      }
    )
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    userid = wx.getStorageSync("userId");
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
    })
    this.getall()
    this.gethuanxinglist()
    this.getbannertext()
    this.getRedpkg()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var imageUrl = getApp().globalData.shareurl
    return {
      title: wx.getStorageSync("userInfo").nickName + '邀您一起听 《天天必听》',
      path: "/pages/style/style" + "?userId=" + wx.getStorageSync("userId"),
      imageUrl: imageUrl,
      success(res) {},
      fail(e) {},
      //分享完成
      complete() {}
    }
  }
})